extends Node2D

# 房间类型
enum ROOM_TYPE {
	FIGHT = 0, 	## 战斗房间
	REWARD = 1,	## 奖励房间
	STORE = 2,	## 商店
	BOSS = 3,	## BOSS
	HOME = 4	## 玩家出生点
}

var enemyTotal = 0				## 敌人总数量
var enemyNum = 0				## 敌人存活数量
var doorIsOpen = false			## 大门是否开启标志
var door = []

@export var level = 0						## 等级越高, 怪物越多
@export var enableDoor = []					## 房间激活的门, 房间不一定四个门都开启, 0: top, 顺时针顺序
@export var roomType = ROOM_TYPE.FIGHT		## 房间类型, 默认战斗类型

# 所有敌人组
@onready var enemies = [
	load("res://Enemy/tscn/Bat.tscn"),
	load("res://Enemy/tscn/Marksman.tscn")
	#load("res://Enemy/tscn/Mech.tscn")
]



@onready var tileMap = $TileMap

# 门模板和门脚本
@onready var doorSource = load("res://Room/tscn/Door.tscn")
@onready var doorScript = load("res://Room/scripts/Door.gd")
@onready var util = Util.new()
# 网络请求工具类
@onready var api = Api.new()

func _ready():
	# 不管哪个模式都要先生成门
	generateDoor()
	match roomType:
		ROOM_TYPE.FIGHT:
			fightRoom()
		
		ROOM_TYPE.REWARD:
			rewardRoom()
			
		ROOM_TYPE.STORE:
			pass
		
		ROOM_TYPE.BOSS:
			bossRoom()
		
		ROOM_TYPE.HOME:
			pass


func bossRoom():
	# 默认boss节点都在第一个位置
	var boss = get_child(0)
	# 绑定boss死亡事件
	util.findOne("$[State]", boss).connect("death", enemyDeath)
	enemyNum = 1
	enemyTotal = 1
	# 执行boss自定义的攻击方式
	boss.attack()
	
	
func rewardRoom():
	openDoor()


'''
	战斗房间:
		根据提供的房间等级, 生成不同数量强度的怪物
'''
func fightRoom():
	# 创建随机数种子
	var random = RandomNumberGenerator.new()
	random.randomize()
	
	# 随机生成房间等级
	level = random.randi_range(0, 4)
	
	enemyNum = changeLevel(random)
	enemyTotal = enemyNum
	# 生成随机个数的敌人
	for i in range(enemyNum):
		# 获得随机的敌人类型
		var enemyIndex = random.randi_range(0, len(enemies) - 1)
		var enemy = enemies[enemyIndex].instantiate()
		# 随机设置敌人位置
		enemy.position = Vector2(random.randi_range(50, 430), random.randi_range(35, 235))
		self.add_child(enemy)
		
		# 获得敌人的state对象
		# 绑定每个敌人的死亡事件
		util.findOne("$[State]", enemy).connect("death", enemyDeath)


'''
	根据难度生成怪物数量
	@param randomObj 随机对象
'''
func changeLevel(randomObj):
	match level:
		0:
			return randomObj.randi_range(1, 5)
			
		1:
			return randomObj.randi_range(5, 10)
		
		2:
			return randomObj.randi_range(10, 15)
			
		3:
			return randomObj.randi_range(15, 20)
			
		4:
			return randomObj.randi_range(20, 25)


# 生成门
func generateDoor():
	var temp = doorScript.new()
	# 每个门位置的单独配置, bottom是代表这个门方块的底部可以被触发
	# 而实际上这个门是上面的门
	var doorConfig = [
		{
			"activeDirection": temp.DIRECTION.BOTTOM, 
			"vector": Vector2(240, 23),
			"rotation": 0
		},
		{
			"activeDirection": temp.DIRECTION.LEFT, 
			"vector": Vector2(442, 125),
			"rotation": 90
		},
		{
			"activeDirection": temp.DIRECTION.TOP, 
			"vector": Vector2(240, 249),
			"rotation": -180
		},
		{
			"activeDirection": temp.DIRECTION.RIGHT, 
			"vector": Vector2(40, 125),
			"rotation": -90
		}
	]
	
	door.clear()
	if len(enableDoor) > 0:
		# 若设定了开门方向, 则按设定进行
		for item in enableDoor:
			var doorNode = doorSource.instantiate()
			# 绑定脚本
			doorNode.set_script(doorScript)
			doorNode.position = doorConfig[item].vector
			doorNode.activeDirection = doorConfig[item].activeDirection
			doorNode.rotation_degrees = doorConfig[item].rotation
			# 默认禁用门的碰撞体
			var collisionShape2D = util.findOne("$[CollisionShape2D]", util.findOne("$[Area2D]", doorNode))
			collisionShape2D.set_deferred("disabled", true)
			# 加载关门动画
			var animation = util.findOne("$[AnimatedSprite2D]", doorNode)
			animation.play("close")
			# 将门添加到房间
			self.add_child(doorNode)
			door.append(doorNode)
	else:
		# 否则四个门全部开启
		for item in doorConfig:
			var doorNode = doorSource.instantiate()
			# 绑定脚本
			doorNode.set_script(doorScript)
			doorNode.position = item.vector
			doorNode.activeDirection = item.activeDirection
			doorNode.rotation_degrees = item.rotation
			
			# 默认禁用门的碰撞体
			var collisionShape2D = util.findOne("$[CollisionShape2D]", util.findOne("$[Area2D]", doorNode))
			collisionShape2D.set_deferred("disabled", true)
			# 加载关门动画
			var animation = util.findOne("$[AnimatedSprite2D]", doorNode)
			animation.play("close")
			
			# 将门添加到房间
			self.add_child(doorNode)
			door.append(doorNode)

## 开门
func openDoor():
	for item in door:
		# 默认禁用门的碰撞体
		var collisionShape2D = util.findOne("$[CollisionShape2D]", util.findOne("$[Area2D]", item))
		collisionShape2D.set_deferred("disabled", false)
		# 加载关门动画
		var animation = util.findOne("$[AnimatedSprite2D]", item)
		animation.play("open")
	
'''
	敌人死亡, 人数-1
'''
func enemyDeath():
	enemyNum -= 1
	if enemyNum <= 0:
		# 记录杀怪数
		api.updateOrCreate(
			self,
			get_tree().root.get_child(0).mac,
			{
				"killCount": enemyTotal
			},
			func (_data):
				pass
		)
		openDoor()
